package com.cliang.servlet;

import com.cliang.entity.BlogUser;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class LoginOutServlet extends HttpServlet {
    private ServletContext servletContext=null;
    private ArrayList<BlogUser> loginedUsers=null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("test/html;charset=UTF-8");

        HttpSession session=req.getSession();
        servletContext=this.getServletContext();
        loginedUsers= (ArrayList<BlogUser>) servletContext.getAttribute("blogUsers");
        loginedUsers.remove( session.getAttribute("blogUser"));
        servletContext.setAttribute("blogUsers",loginedUsers);


        //清除session数据
        session.invalidate();
        PrintWriter printWriter=resp.getWriter();
        printWriter.print("<script language=javascript> alert('您已成功注销系统！');</script>");

        req.getRequestDispatcher("/Blog/login/login.html").forward(req,resp);
        printWriter.close();
    }
}
